<template>
  <header class="header">
    <h1>教育平台录播课商城</h1>
    <nav class="header-nav" v-if="!user.isAuthenticated">
      <ul>
        <li><router-link to="/login">登录</router-link></li>
        <li><router-link to="/register">注册</router-link></li>
      </ul>
    </nav>
    <nav class="header-nav" v-else>
      <ul>
        <li><router-link to="/OrderManagement">我的订单</router-link></li>
        <li><router-link to="/support">在线客服</router-link></li>
        <li><router-link to="/help">帮助中心</router-link></li>
        <li @click="logout">退出登录</li>
      </ul>
    </nav>
    <div class="header-right">
      <input type="text" placeholder="输入关键字进行搜索">
      <button class="search-btn">搜索</button>
      <div class="cart-icon" @click="$emit('openCart')">
        🛒
        <span class="badge">9</span>
      </div>
    </div>
  </header>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from '@/store'

const router = useRouter()
const store = useStore()

const user = computed(() => store.user)

const logout = () => {
  store.logout()
  router.push('/')
}
</script>

<style scoped>
/* 保持原有样式不变 */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  background-color: #f5f5f5;
}

.header-nav {
  flex: 1; 
  text-align: right;
}

.header-nav ul {
  list-style: none;
  display: inline-flex;
  gap: 20px;
  padding: 0;
  margin: 0;
}

.header-nav li {
  position: relative;
  cursor: pointer;
  transition: all 0.2s ease;
}

.header-nav a {
  color: inherit;
  text-decoration: none;
  font-weight: bold;
}

.header-nav a:hover {
  color: #ff6b6b;
}

.header-nav li:not(:last-child)::after {
  content: "|";
  margin-left: 20px;
  color: #ccc;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.search-btn {
  padding: 8px 16px;
  background-color: transparent;
  border: 2px solid #ccc;
  color: #666;
  cursor: pointer;
  transition: all 0.2s ease;
}

.search-btn:hover {
  background-color: #eee;
}

.cart-icon {
  position: relative;
  margin-left: 10px;
  cursor: pointer;
  transition: color 0.2s ease;
}

.cart-icon:hover {
  color: #ff6b6b;
}

.badge {
  position: absolute;
  top: -6px;
  right: -6px;
  background-color: red;
  color: white;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
}
</style>